﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Extensions.FileProviders;
using SysIO = System.IO;
using System.Collections.Generic;
using VOL.System.Controllers;

namespace VOL.WebApi.Controllers
{
    [ApiController]
    [Route("api/files")]
    [ApiExplorerSettings(GroupName = "v3")]
    public class FilesController : ControllerBase
    {
        private readonly FileHelper _fileHelper;

        public FilesController(FileHelper fileHelper)
        {
            _fileHelper = fileHelper;
        }

        [HttpPost, Route("GetFileList")]
        public IActionResult GetFileList([FromBody] string remoteFolderPath = "")
        {
            // 在这里编写代码获取文件列表，并返回数据
            var fileList = GetFiles(remoteFolderPath);
            return new JsonResult(fileList);
        }

        private List<TreeNode> GetFiles(string remoteFolderPath = "")
        {
            List<TreeNode> fileList = new List<TreeNode>();

            var directoryContents = _fileHelper.GetFileList(remoteFolderPath);

            foreach (var ftpListItem in directoryContents)
            {
                if (ftpListItem.IsDirectory)
                {
                    //显示文件夹
                    var item = new TreeNode() { Title = ftpListItem.Name, Field = "文件夹", Href = remoteFolderPath + "/" + ftpListItem.Name };
                    fileList.Add(item);

                }
                else
                {
                    //显示文件
                    var item = new TreeNode() { Title = ftpListItem.Name, Field = "文件", Href = remoteFolderPath + "/" + ftpListItem.Name };
                    fileList.Add(item);
                    //Console.WriteLine($"[F] {ftpListItem.Name}");
                }
            }

            //client.Disconnect();
            //}

            return fileList;
        }


        [HttpPost("upload")]
        public async Task<IActionResult> UploadFile(IFormFile file, string? uploadFolder)
        {
            Dictionary<bool, string> result = await _fileHelper.UploadFile(file, uploadFolder);
            if (!result.ContainsKey(true))
            {
                return BadRequest("无效的文件");
            }
            return Ok("文件上传成功");
        }

        [HttpPost("download")]
        public IActionResult DownloadFile([FromBody]string fileName)
        {
            byte[] fileBytes = _fileHelper.DownloadFile(fileName);

            if (fileBytes.Length>0)
            {
                return File(fileBytes, "application/octet-stream", fileName);
            }

            return NotFound();
        }

        [HttpPost("createfolder")]
        public IActionResult CreateFolder([FromBody] string folderPath)
        {
            Dictionary<bool,string> result = _fileHelper.CreateFolder(folderPath);

            if (result.ContainsKey(true))
            {
                return Ok(result[true]);
            }

            return Conflict(result[false]);
        }

        [HttpPost("folderexists")]
        public IActionResult FolderExists([FromBody] string folderPath)
        {
           var results = _fileHelper.FolderExists(folderPath);
            return Ok(new { Exists = results });
        }
    }

}
